<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 李邦琦 <1536549251@qq.com>
// +----------------------------------------------------------------------

namespace app\crm\controller;
use think\Loader;
use think\Cache;

class Index extends CrmController
{
    /**
     * 选择需要登录验证的方法名 直接排除过他妈的居然不行,非要老子这样写
     * @var [arr]
     */
    protected $beforeActionList = [
        'checkLogin' => ['only'=>'index,logout,personalInfo'],
    ];


    /**
     * 模板输出
     */
    public function index()
    {
        // echo 222;exit;
		$user_info = $_SESSION['user_info'];
        $arrMenu = Loader::model('Index')->index();
        $this->assign("user_info",$user_info);
		$this->assign("menu",$arrMenu);
        // 模板输出
        return $this->fetch('index');
    }


    /**
     * 模板输出
     */
    public function login()
    {
        // echo 111;exit;
        //return view();
        // 模板输出
        // return $this->fetch(APP_PATH.request()->module().'/view/Index/login.html');
        return $this->fetch('login');
    }


    /**
     * 模板输出
     */
    public function personalInfo()
    {
        $username = $_SESSION['user_info']["username"];
        $userInfo = Loader::model('Users')->getUserInfo($username);
        $this->assign('userInfo',$userInfo);    
        $roleList = Loader::model('Role')->getRoleList();
        $this->assign('roleList',$roleList);
        $result = Loader::model('Department')->recursionDepartment();
        $result = Loader::model('Department')->recursionDepartmentArr($result,$userInfo["d_id"]);
        //dump($result);die;
        $this->assign('options', $result);
        //return view();
        // 模板输出
        return $this->fetch('personalInfo');
    }


    /**
     * 模板输出
     */
    public function welcome()
    {
        $role = Cache::get('role');
        $user_role = $role[$_SESSION['user_info']['r_id']]['r_name'];
        $this->assign('user_role',$user_role);

        $pictureList = Loader::model('Index')->pictureData();
        $this->assign('pictureList',$pictureList);

        // 模板输出
        return $this->fetch('welcome');
    }


    /**
     * 模板输出
     */
    public function setTarget()
    {
        $this->assign('type',$_REQUEST['type']);
        // 模板输出
        return $this->fetch('setTarget');
    }


    public function doSetTarget () 
    {
        Loader::model('Index')->doSetTarget();
    }


    public function getTarget () 
    {
        Loader::model('Index')->getTarget();
    }


    /**
     * 用户登录
     * @return [json] [登录成功/失败]
     */
    public function userLogin()
    {
        // 获取验证信息
        $username = $_REQUEST['username'];
        $password = $_REQUEST['password'];
        $data = array(
            'username' => $username,
            'password' => $password,
        );
        // 验证登录
        $result = Loader::model('Index')->userLogin($data);
        // 返回结果集
        if ($result === true){
            echo json_encode(array('success'=>true,'msg'=>'登录成功！'));
            // var_dump($_SESSION);
        } else {
            echo json_encode(array('msg'=>$result));
        }
    }


    public function logout () 
    {
        unset($_SESSION['user_info']);
        unset($_SESSION['user_priv']);
        setcookie('login_mark', $result['login_mark'], -1, '/'); // 存入cookie login_mark
        setcookie('username', $result['username'], -1, '/'); // 存入cookie username
        setcookie('password', $result['password'], -1, '/'); // 存入cookie password
        echo true;
    }

	public function settingPassword()
	{
		Loader::model('Index')->settingPassword();
		
	}


    public function realtimeData ()
    {
        Loader::model('Index')->realtimeData();
    }


    public function orderData ()
    {
        Loader::model('Index')->orderData();
    }


    public function customerGroupCount ()
    {
        Loader::model('Index')->customerGroupCount();
        
    }


}
